﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using WebApplication5.Model;

namespace WebApplication5.Controller
{
    public class taskCtr
    {

        dmaa0912DannyEntities2 db = new dmaa0912DannyEntities2();

            public Task GetTask(int id)
            {
                var q = (from t in db.Task
                         where t.id == id
                         select t);

                return q.FirstOrDefault();
            }

            public void AddTask(Task t)
            {

                validationOfTask(t);

                db.Task.Add(t);
                db.SaveChanges();
            }

            public void editTask(Task taskUpdate)
            {
                validationOfTask(taskUpdate);

                Task task = db.Task.FirstOrDefault(h => h.id == taskUpdate.id);
                task.name = taskUpdate.name;
                task.status = taskUpdate.status;
                task.type = taskUpdate.type;
                task.Userstory = taskUpdate.Userstory;
                task.userstoryId = taskUpdate.userstoryId;

                db.SaveChanges();
            }

            public List<Task> GetAllTasks()
            {
                try
                {
                    var tasks = from u in db.Task
                                   select u;

                    return tasks.ToList();
                }
                catch (Exception e)
                {

                }
                return null;
            }


            public void deleteTask(int id)
            {
                Task u = (from v in db.Task
                             where v.id == id
                             select v).FirstOrDefault();

                db.Task.Remove(u);

                db.SaveChanges();
            }


            public List<Task> GetAllTaskId(int id)
            {
                try
                {
                    var tasks = from u in db.Task
                                      where id == u.userstoryId
                                      select u;

                    return tasks.ToList();
                }
                catch (Exception e)
                {

                }
                return null;
            }

            public List<Task> GetAllTaskIdSame(int id)
            {
                try
                {
                    var tasks = from u in db.Task
                                where id == u.id
                                select u;

                    return tasks.ToList();
                }
                catch (Exception e)
                {

                }
                return null;
            }

            private void validationOfTask(Task t)
            {

                Regex onlyDigits = new Regex("^[0-9]+$");
                Regex onlyLetters = new Regex(@"^[a-zA-Z]+$");
                Regex onlyLettersAndNumbers = new Regex(@"[a-zæøå 0-9]+");

                if (!onlyDigits.IsMatch(t.name))
                    throw new Exception("Kun bogstaver tilladt i navn");

                if (string.IsNullOrEmpty(t.status))
                    throw new Exception("Indtast kun bogstaver i status");

                if (string.IsNullOrEmpty(t.type))
                    throw new Exception("Indtast kun bogstaver i type");

            }
        }
    
}